package com.mlick.a.other;

import org.junit.Test;
import redis.clients.jedis.*;

import java.util.HashSet;
import java.util.Set;

/**
 * @author lixiangxin
 * @date 2019/4/2 11:03
 */
public class BatchDeleteRedisTest {

  Set<HostAndPort> jedisClusterNodes = new HashSet<>();
  JedisCluster jc;

  //  @Before
  public void init() {
    jedisClusterNodes.add(new HostAndPort("10.27.21.243", 7000));
    jedisClusterNodes.add(new HostAndPort("10.27.21.243", 7001));
    jedisClusterNodes.add(new HostAndPort("10.27.21.243", 7002));

    jc = new JedisCluster(jedisClusterNodes, 3000, 2);
  }

  private void del(int p) {
    del(p, "apim:*");
  }

  private void del(int p, String key) {
    Jedis jedis = new Jedis("10.65.18.85", p, 6000);

    String s1 = jedis.get(key);
    System.out.println(s1);

    Set<String> set = jedis.keys(key);

    System.out.println(set.size());
    for (String s : set) {
      System.out.println(s);
      //      long r = 0;
      //      try {
      //        r = jc.del(s);
      //      } catch (Exception ignored) {
      //      }
      //      System.out.println(r);
    }
  }

  public static void main(String[] args) {
    BatchDeleteRedisTest b = new BatchDeleteRedisTest();
    //    b.init();
    //    new Thread(() -> b.del(7000)).start();
    //    new Thread(() -> b.del(7001)).start();
    //    new Thread(() -> b.del(7002)).start();
    //     b.del(6379,"apim:api:778");
    //    delKeys();
  }

  private static final int PAGE_COUNT = 5000;

  @Test
  public void delKeys() {
    // ppe
    // Jedis jedis = new Jedis("10.65.18.85", 6379, 15000);

    //beta
    Jedis jedis = new Jedis("10.27.20.115", 6380, 15000);

    ScanParams scanParams = new ScanParams();
    scanParams.match("apim:token:refresh:*");
//    scanParams.match("apim:token:access:*");
    scanParams.count(PAGE_COUNT);

    //    File backUpFile = Paths.get("D:\\backup-token-refresh.txt").toFile();
    //    AtomicInteger count = new AtomicInteger(0);

    String cousor = "0";
    for (int i = 0; i < 20; i++) {
      Long delCount = 0L;
      try {
        ScanResult<String> scanResult = jedis.scan(cousor, scanParams);
        cousor = scanResult.getCursor();
        System.out.println("update after=>" + cousor);
        delCount = jedis.del(scanResult.getResult().toArray(new String[0]));
      } catch (Exception ignored) {
      }
      System.out.println(i + ":" + delCount);
      //      scanResult
      //          .getResult()
      //          .forEach(
      //              key -> {
      //
      //                String value = jedis.get(key);
      //                //                System.out.println(key);
      //                //                System.out.println(value);
      //                try {
      //                  FileUtils.writeLines(backUpFile, Arrays.asList(key, value), true);
      //                } catch (IOException e) {
      //                  e.printStackTrace();
      //                }
      //                Long del = jedis.del(key);
      //
      //                System.out.println("delete " + (del > 0 ? "success" : "fail"));
      //
      //                System.out.println("process count => " + count.getAndIncrement());
      //              });
    }
  }

//  public void t1() {
//
//    String key = "apim:" + Constants.REDIS_API_CATALOG + 252;
//    byte[] values = jc.get(key.getBytes());
//
//    JSONObject jsonObject = KryoSerializerUtil.deserializeByClassType(values, JSONObject.class);
//
//    System.out.println(jsonObject.toJSONString());
//  }

  //  @Test
  public void t2() {
    del(7000, "apim:token:appKey:*");
    del(7001, "apim:token:appKey:*");
    del(7002, "apim:token:appKey:*");
    del(7000, "apim:token:access:*");
    del(7001, "apim:token:access:*");
    del(7002, "apim:token:access:*");
  }
}
